﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using ScottIsAFool;

namespace SignaturePlugin
{
    public partial class frmOptions : Form
    {
        Settings m_settings;
        string[] m_signatures;
        string[] m_blognames;
        bool m_edited;
        int previousselected;

        public frmOptions(Settings settings)
        {
            InitializeComponent();
            m_settings = settings;
            LoadBlogs();
            m_signatures = GetSignatures();
            if (lbBlogs.SelectedIndex < 0)
            {
                txbxSignature.Enabled = false;
            }
        }

        private string[] GetSignatures()
        {
            List<string> temp = new List<string>();
            foreach (string b in m_blognames)
            {
                string sig = string.Empty;
                foreach (string s in m_settings.SettingValues)
                {
                    string settingname = string.Format("({0}).SignatureText", b);
                    if (s.StartsWith(settingname))
                    {
                        m_settings.settingname = settingname;
                        sig = m_settings.Signature;
                    }
                }
                if (string.IsNullOrEmpty(sig))
                    temp.Add("");
                else
                    temp.Add(sig);
            }

            return temp.ToArray();
        }

        private void LoadBlogs()
        {
            List<string> blogs = new List<string>();
            //RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows Live\Writer\Weblogs", false);
            //foreach (string r in key.GetSubKeyNames())
            //{
            //    RegistryKey blog = key.OpenSubKey(r, false);
            //    string blogname = blog.GetValue("BlogName").ToString();
            //    blogs.Add(blogname);
            //    lbBlogs.Items.Add(blogname);
            //}
            foreach (Blog b in WriterUtils.ConfiguredBlogs())
            {
                blogs.Add(b.Name);
                lbBlogs.Items.Add(b.Name);
            }
            m_blognames = blogs.ToArray();
        }

        private void btnSaveAll_Click(object sender, EventArgs e)
        {
            Save();
            m_edited = false;
            btnSaveAll.Enabled = false;
        }

        private void Save()
        {
            if (m_edited)
            {
                int i = 0;
                foreach (string b in m_blognames)
                {
                    string sig = m_signatures[i];
                    string settingname = string.Format("({0}).SignatureText", b);
                    m_settings.settingname = settingname;
                    m_settings.Signature = sig;
                    i++;
                }
            }
        }

        private void lbBlogs_SelectedIndexChanged(object sender, EventArgs e)
        {
            txbxSignature.Enabled = true;
            string newsig = txbxSignature.Text;
            if (m_edited)
            {
                TempSave(newsig);
            }
            string sig = m_signatures[lbBlogs.SelectedIndex];
            txbxSignature.Text = sig;
            previousselected = lbBlogs.SelectedIndex;
        }

        private void TempSave(string newsig)
        {
            m_signatures[previousselected] = newsig;
        }

        private void txbxSignature_TextChanged(object sender, EventArgs e)
        {
            int i = lbBlogs.SelectedIndex;
            string s = m_signatures[i];
            if (!s.Equals(txbxSignature.Text))
            {
                m_edited = true;
                btnSaveAll.Enabled = true;
                TempSave(txbxSignature.Text);
            }
            
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (m_edited)
            {
                DialogResult result = MessageBox.Show("There have been edits made to the signatures, are you sure you wish to close?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (!e.Cancel && (result == DialogResult.Yes))
                    e.Cancel = false;
                else
                    e.Cancel = true;
            }
            base.OnClosing(e);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Save();
            this.Close();
        }

        private void lnkClear_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txbxSignature.Text = string.Empty;
        }
    }
}
